package terra.model.object;

import terra.managers.ThreadPoolManager;
import terra.model.Formulas;
import terra.model.object.interfaces.Attackable;
import terra.model.object.interfaces.Attacker;
import terra.model.object.stat.CharacterStat;
import terra.model.runnable.ObjectRemover;

public abstract class LiveObject extends MovableObject implements Attackable {

	private static final long serialVersionUID = 2800765814684733021L;
	private boolean dead = false;
	protected int removeCooldown = 1000;
	protected CharacterStat stat;

	public LiveObject(int objectId) {
		super(objectId);
	}

	@Override
	public void onHit(Attacker damager, float damage) {
		if (!getStat().reduceHp(
				(int) Formulas.calcDamage((GameObject) damager, this, damage))
				&& !isDead())
			die(damager);
	}

	@Override
	public void die(Attacker killer) {
		getStat().setCurrentHp(0);
		setDead(true);
		if (killer != null)
			killer.onKill(this);
		if (!(this instanceof Player))
			ThreadPoolManager.getInstance().scheduleGeneral(
					new ObjectRemover(this), 1500L);
	}

	public boolean isDead() {
		return dead;
	}

	public void setDead(boolean dead) {
		this.dead = dead;
	}

	public CharacterStat getStat() {
		if (stat == null)
			stat = new CharacterStat(this);
		return stat;
	}

}
